package com.smart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.entity.Article_Class;
import com.smart.entity.Article_List;
import com.smart.entity.Label;
import com.smart.mapper.ClassMapper;
import com.smart.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ClassServiceImpl implements ClassService {
    @Autowired
    ClassMapper classMapper;

    @Override
    public Map<String, Object> ArticleClassAll(Integer currentPage, Integer pageSize) {
        Page<Article_Class> page = new Page<>(currentPage,pageSize);//查询的N页，显示N条数据
        QueryWrapper<Article_Class> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("class_id","class_name");
        IPage<Article_Class> iPage = classMapper.selectPage(page, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("current",iPage.getCurrent());
        map.put("data", iPage.getRecords());
        return map;
    }


    @Override
    public int ArticleClassAdd(String classname) {
        QueryWrapper<Article_Class> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("class_name",classname);
        Long aLong = classMapper.selectCount(queryWrapper);
        if (aLong > 0) {
            return 0;
        }

        Article_Class article_class = new Article_Class();
        article_class.setClassName(classname);
        System.out.println(article_class);
        int result = classMapper.insert(article_class);
        return result;
    }

    @Override
    public int ArticleClassDelete(Integer id) {
        Article_Class article_class = new Article_Class();
        article_class.setClassId(id);
        int result = classMapper.deleteById(article_class);
        return result;
    }

    @Override
    public Article_Class ArticleByClass(Integer id) {
        Article_Class articleClass = classMapper.selectById(id);
        return articleClass;
    }

    @Override
    public Integer ArticleClassEdit(Article_Class temp) {
        QueryWrapper<Article_Class> labelQueryWrapper = new QueryWrapper<>();
        labelQueryWrapper.eq("class_id",temp.getClassId());
        Integer i = classMapper.update(temp, labelQueryWrapper);
        return i;
    }
}
